/**
 * Looker  新闻整合系统
 * Author  :solosky
 * File    :MediaDisposer.java
 * Date    :May 21, 2009
 * Lisence : Apache License 2.0 
 */
package net.looker.monitor.disposer;

import net.looker.data.Media;
import net.looker.util.URLUtils;

/**
 * 新闻文件处理
 * @author solosky
 *
 */
public class MediaDisposer extends Disposer
{
	/**
	 * 需处理的文件
	 */
	private Media media;
	
	public MediaDisposer(Media media)
	{
		this.media = media;
	}
	
	/* (non-Javadoc)
	 * @see net.looker.monitor.disposer.Disposer#dispose()
	 */
	@Override
	public void dispose() 
	{
		media.setName(URLUtils.file(media.getUrl()));
		media.setExt( fileExt(media.getName()));
		media.setType(fileType(media.getExt()));
	}
	
	
	/**
	 * 返回文件扩展名
	 * @param fileName
	 * @return
	 */
	public static  String fileExt(String fileName)
	{
		int start = fileName.lastIndexOf('.');
		if (start==-1)
			return fileName;
		else
			return fileName.substring(start+1);
	}

	/**
	 * 返回文件类型
	 * @param ext
	 * @return
	 */
	public static Media.ETYPE fileType(String ext)
	{
		String imageType = "jpg,png,gif,bmp";
		String videoType = "rmvb,wmv";
		String audioType = "mp3,wma";
		String zipType   = "zip,rar,7z,bz,tar";
 		String docType   =  "doc,xls,ppt,docx,xlsx,pptx,pdf";
		
		if(imageType.indexOf(ext)!=-1)
			return Media.ETYPE.i;
		else if(videoType.indexOf(ext)!=-1)
			return Media.ETYPE.v;
		else if (audioType.indexOf(ext)!=-1)
			return Media.ETYPE.a;
		else if(docType.indexOf(ext)!=-1)
			return Media.ETYPE.d;
		else if(zipType.indexOf(ext)!=-1)
			return Media.ETYPE.z;
		else
			return Media.ETYPE.o;
	}

}
